package com.shundr.shipper.common.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.common.model.HistoryInfo;
import com.shundr.shipper.common.util.CommonConfig;
import com.shundr.shipper.common.util.DateUtil;

/**
 * 
 * 历史数据适配器
 * 
 * @author larry 2015-4-13 下午4:26:47 
 * @version 1.0.0
 *
 */
public class HistoryAdapter extends BaseAdapter{
	public List<HistoryInfo> mList;
	private Context mContext;

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int index) {
		return mList.get(index);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	public HistoryAdapter(List<HistoryInfo> list, Context mContext) {
		super();
		this.mList = list;
		this.mContext = mContext;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		ViewHold viewHold = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_history,null);
			viewHold = new ViewHold();
			viewHold.tvObjType = (TextView) convertView.findViewById(R.id.tv_object_type);
			viewHold.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
			viewHold.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			viewHold.tvOpType = (TextView) convertView.findViewById(R.id.tv_op_type);
			convertView.setTag(viewHold);
		}else{
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			HistoryInfo info = mList.get(position);
			switch (info.getObjectType()) {
			case CommonConfig.OPERATE_OBJECT_CARGO:
				viewHold.tvObjType.setText("货源");
				break;
			case CommonConfig.OPERATE_OBJECT_TRUCK:
				viewHold.tvObjType.setText("车源");
				break;
			case CommonConfig.OPERATE_OBJECT_OWNER:
				viewHold.tvObjType.setText("货主");
				break;
			case CommonConfig.OPERATE_OBJECT_TRUCKER:
				viewHold.tvObjType.setText("车主");
				break;

			default:
				break;
			}
			viewHold.tvTime.setText(DateUtil.getInterval(info.getOperateTime()));
			viewHold.tvTitle.setText(info.getTitle().replace("null", ""));
			switch (info.getOperateType()) {
			case CommonConfig.OPERATE_WATCH:
				viewHold.tvOpType.setText( "详情");
				viewHold.tvOpType.setBackgroundResource(R.drawable.circle_text_detail);
				break;
			case CommonConfig.OPERATE_CALL:
				viewHold.tvOpType.setText( "电话");
				viewHold.tvOpType.setBackgroundResource(R.drawable.circle_text_call);
				break;
			case CommonConfig.OPERATE_FAVORITE:
				viewHold.tvOpType.setText( "收藏");
				viewHold.tvOpType.setBackgroundResource(R.drawable.circle_text_fav);
				break;
			case CommonConfig.OPERATE_SERVICE:
				viewHold.tvOpType.setText( "服务");
				viewHold.tvOpType.setBackgroundResource(R.drawable.circle_text_service);
				break;
			case CommonConfig.OPERATE_ILLEGAL:
				viewHold.tvOpType.setText( "违章");
				viewHold.tvOpType.setBackgroundResource(R.drawable.circle_text_illegal);
				break;
			case CommonConfig.OPERATE_GAME:
				viewHold.tvOpType.setText( "娱乐");
				viewHold.tvOpType.setBackgroundResource(R.drawable.circle_text_game);
				break;

			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHold{
		TextView tvObjType;
		TextView tvTime;
		TextView tvTitle;
		TextView tvOpType;
	}
}
